package client.comm.baoding.ui

import android.content.Intent
import androidx.lifecycle.Observer
import client.comm.baoding.R
import client.comm.baoding.adapter.page.Zz2FragmentPageAdapter
import client.comm.baoding.api.event.EventUserInfo
import client.comm.baoding.databinding.ActivityZz2Binding
import client.comm.baoding.ui.vm.Zz2ViewModel
import client.comm.commlib.base.BaseActivity
import client.comm.commlib.toast.toast
import org.greenrobot.eventbus.EventBus


class Zz2Activity : BaseActivity<Zz2ViewModel, ActivityZz2Binding>() {

    override fun getLayout(): Int {
        return R.layout.activity_zz2
    }

    val adapter: Zz2FragmentPageAdapter by lazy {
        Zz2FragmentPageAdapter(supportFragmentManager)
    }

    override fun initViewModel(): Zz2ViewModel = createViewModel()

    override fun initView() {
        binding.apply {
            event = this@Zz2Activity
            vm = viewModel
        }

        binding.viewPager.adapter = adapter
        binding.tabLayout.setupWithViewPager(binding.viewPager)

        viewModel.call.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("操作成功")
                    startActivity(Intent(this@Zz2Activity, MxJlActivity::class.java).apply {
                        putExtra("index", 1)
                    })
                    EventBus.getDefault().post(EventUserInfo())
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })

//        when (intent.getIntExtra("index", 0)) {
//            0 -> {
////                binding.tabWrap.getChildAt(0).isSelected = true
//            }
//            1 -> {
//                binding.viewPager.postDelayed({
//                    binding.viewPager.setCurrentItem(1, false)
//                }, 20)
//            }
//            2 -> {
//                binding.viewPager.postDelayed({
//                    binding.viewPager.setCurrentItem(2, false)
//                }, 20)
//            }
//        }

    }

    fun jl() {
        startActivity(Intent(this, MxJlActivity::class.java).apply {
            putExtra("index", 1)
        })
    }

}